package com.matt.controller.web;

import com.google.gson.Gson;
import com.matt.commons.model.StatusResponse;
import org.junit.Assert;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * @author diao.qiao@outlook.com
 * @version V1.0
 * @description: mockMvc工具
 * @since 2019-01-11
 */
public class MockMvcUtil {

    /**
     * controller 测试工具，不含请求头
     *
     * @param wac          webApplicationContext
     * @param url          请求地址
     * @param params       请求参数
     * @param expectedCode 预期返回的自定义code
     * @throws Exception
     */
    static void test(WebApplicationContext wac, String url, MultiValueMap<String, String> params, String expectedCode) throws Exception {
        MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();  //通过WebApplicationContext创建mockMvc
        String content = mockMvc.perform(
                post(url)
                        .contentType(MediaType.APPLICATION_FORM_URLENCODED)  //数据的格式
                        .params(params)         //添加参数
        )
                .andExpect(status().isOk()  //返回状态 200
                )
                .andDo(print()              //打印请求
                )
                .andReturn().getResponse().getContentAsString();//将相应的数据转换为字符串
        StatusResponse response = new Gson().fromJson(content, StatusResponse.class);
        Assert.assertEquals("Test失败", expectedCode, response.getCode());
    }

    /**
     * controller 测试工具，包含请求头
     *
     * @param wac          webApplicationContext
     * @param url          请求URL
     * @param headers      请求头
     * @param params       请求参数
     * @param expectedCode 预期返回的自定义code
     * @throws Exception
     */
    static void testAndHeader(WebApplicationContext wac, String url, HttpHeaders headers, MultiValueMap<String, String> params, String expectedCode) throws Exception {
        MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();  //通过WebApplicationContext创建mockMvc
        String content = mockMvc.perform(
                post(url)
                        .contentType(MediaType.APPLICATION_FORM_URLENCODED)  //数据的格式
                        .headers(headers)       //设置header
                        .params(params)         //添加参数
        )
                .andExpect(status().isOk()  //返回状态 200
                )
                .andDo(print()              //打印请求
                )
                .andReturn().getResponse().getContentAsString();//将相应的数据转换为字符串
        StatusResponse response = new Gson().fromJson(content, StatusResponse.class);
        Assert.assertEquals("Test失败", expectedCode, response.getCode());
    }
}
